/*menu*/
function formAdd() {
    $('#content').load('home/add', function () {
        initCategorie(getData());
        theSlide();
        geoCoder();
        radioPriorite();
        timePicker();
        datepicker1();
        datepicker2();
        ajout_tache();
        policeTask();
        boutonAdd_HTML("add");
        saveAdd();
    });
}

function formEdit(id){
    $('#content').load('home/add', function () {
		initCategorie(getData());
		theSlide();
		geoCoder();
		radioPriorite();
		timePicker();
		datepicker1();
		datepicker2();
		edit_tache(id,getData());
		policeTask();
		boutonAdd_HTML("edit");
        saveEdit()
	});
}
	
$(function(){
	routes['/add']=formAdd;
	routes['/edit/:id']=formEdit;
});

//script pour bouton priorité task jQuery
function radioPriorite(){
	$( "#radio" ).buttonset();
};
// fin script pour bouton priorité task jQuery

function saveAdd() {
    $("#buttonAdd").click(function () {
        var tache = getTache("add");

        var json = JSON.stringify(tache);

        $.ajax({
            url: 'home/add',
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {

                lesTasks = data;

                location.href = "/#/accueil";
            }
        });
    });
};

function saveEdit() {
    $("#buttonEdit").click(function () {
        var tache = getTache("edit");

        var json = JSON.stringify(tache);

        $.ajax({
            url: 'home/edit',
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function () {
                loadData(function () {
                    location.href = "/#/accueil";
                });
            }
        });
    });
};

function getTache(action) {
    var objet = window.document.getElementById('id_object').value;
    var categ = $('.categ').text();
    var prio1 = window.document.getElementById('radio1').checked;
    var prio2 = window.document.getElementById('radio2').checked;
    var prio3 = window.document.getElementById('radio3').checked;
    var date = window.document.getElementById('datepicker').value;
    var desc = window.document.getElementById('id_desc').value;
    var lieu = window.document.getElementById('location').value;
    //récupération des différents mots dans un tableau
    var tags = window.document.getElementById('id_tags').value.split(",");

    if (prio1 == true) {
        var prio = 75;
        var color = "prio1";
    }
    if (prio2 == true) {
        var prio = 150;
        var color = "prio2";
    }
    if (prio3 == true) {
        var prio = 300;
        var color = "prio3";
    }
    var idTache;
    if (action == "add") {
        idTache = getData().length + 1;
    }
    else {
        idTache = $('.idEdit').text();
    }
    
    var newTask = { "id": idTache, "objet": objet, "priorite": prio, "date": date, "categ": categ, "description": desc, "lieu": lieu, "tags": tags, "couleur": color, "corbeille": 0, "historique": 0, "favori": 0 };

    //lesTasks.push(newTask);
    return newTask;
}